package b1.mobile.blackberry.screen;

import b1.mobile.blackberry.controller.AgentController;
import b1.mobile.blackberry.entity.AppConfiguration;
import b1.mobile.blackberry.ui.component.PreferenceEditField;
import b1.mobile.blackberry.ui.component.PreferenceToggleField;
import b1.mobile.blackberry.ui.component.util.FieldFactory;
import b1.mobile.blackberry.ui.component.util.ThemeUtil;
import net.rim.device.api.system.Display;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.Manager;
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.ui.container.VerticalFieldManager;

public class SettingsScreen extends MainScreen{

	/**
	 * Used for displaying the application configuration.
	 * 
	 * @author I067614
	 */
	public SettingsScreen() {
		super(MainScreen.VERTICAL_SCROLL | MainScreen.VERTICAL_SCROLLBAR);
		this.InitializeUIComponent();
		this.RefreshData();
	}
	
	private PreferenceEditField serverAddress = new PreferenceEditField(Field.USE_ALL_WIDTH);
	private PreferenceToggleField enableSSL = new PreferenceToggleField(Field.USE_ALL_WIDTH);
	private PreferenceEditField companyDB = new PreferenceEditField(Field.USE_ALL_WIDTH);
	private PreferenceEditField userCode = new PreferenceEditField(Field.USE_ALL_WIDTH);
	private PreferenceEditField phoneNum = new PreferenceEditField(Field.USE_ALL_WIDTH);
//	private PreferenceVerticalFieldManager pvfm = new PreferenceVerticalFieldManager(Field.USE_ALL_WIDTH);
	
	ButtonField btnSave = new ButtonField("Save", ButtonField.CONSUME_CLICK | ButtonField.NEVER_DIRTY | Field.FIELD_HCENTER){
		public int getPreferredWidth() {
            return Display.getWidth() - 25;
        }
	};
	ButtonField btnCancel = new ButtonField("Cancel", ButtonField.CONSUME_CLICK | ButtonField.NEVER_DIRTY);
	
	private void InitializeUIComponent() {
		setTitle(FieldFactory.makeScreenTitle("Settings"));
		ThemeUtil.decorateScreen(this);
		
		serverAddress.setLabelText("Server");
		enableSSL.setLabelText("Enable SSL");
		companyDB.setLabelText("Company DB");
		userCode.setLabelText("User Name");
		phoneNum.setLabelText("Phone Num");
		
		Manager mgr = new VerticalFieldManager(USE_ALL_WIDTH);
		mgr.setMargin(10, 0, 10, 0);
		mgr.add(serverAddress);
		mgr.add(enableSSL);
		mgr.add(companyDB);
		mgr.add(userCode);
		mgr.add(phoneNum);		
		
//		add(pvfm);
		
		btnSave.setChangeListener(new FieldChangeListener(){
			public void fieldChanged(Field field, int context) {
				saveConfig();
			}
		});
		btnCancel.setChangeListener(new FieldChangeListener(){
			public void fieldChanged(Field field, int context) {
				resetConfig();
			}
		});
		
		Manager buttonMgr = new VerticalFieldManager(Field.USE_ALL_WIDTH);
		buttonMgr.add(btnSave);
//		buttonMgr.add(btnCancel);
		buttonMgr.setMargin(10, 0, 0, 0);
		mgr.add(buttonMgr);
		add(mgr);
		add(new LabelField());
	}

	private void RefreshData()
	{
		AppConfiguration config = AgentController.Configuration;
		
		serverAddress.setText(config.getServerAddress());
		enableSSL.setValue(config.isSSL());
		companyDB.setText(config.getCompanyDB());
		userCode.setText(config.getUserCode());
		phoneNum.setText(config.getPhoneNum());
	}
	
	private void saveConfig(){
		AppConfiguration config = new AppConfiguration();
		
		config.setServerAddress(serverAddress.getText());
		config.setSSL(enableSSL.getValue());
		config.setCompanyDB(companyDB.getText());
		config.setUserCode(userCode.getText());
		config.setPhoneNum(phoneNum.getText());
		
		AgentController.SaveConfiguration(config);
		
		this.close();
	}
	
	private void resetConfig() {
		this.close();
	}
}
